package com.fs.sw.utils;

import org.hibernate.service.spi.ServiceException;

import java.sql.Time;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class CalendarUtil {

	public static int getYearForAgeByBirth(Date birthday) {
		int age = 0;
		try {
			Calendar now = Calendar.getInstance();
			now.setTime(new Date());// 当前时间

			Calendar birth = Calendar.getInstance();
			birth.setTime(birthday);

			if (birth.after(now)) {// 如果传入的时间，在当前时间的后面，返回0岁
				age = 0;
			} else {
				age = now.get(Calendar.YEAR) - birth.get(Calendar.YEAR);
			}
			return age;
		} catch (Exception e) {// 兼容性更强,异常后返回数据
			return 0;
		}
	}
	
	private static int getMonthForAgeByBirth(Date birthday) {
		int age = 0;
		try {
			Calendar now = Calendar.getInstance();
			now.setTime(new Date());// 当前时间

			Calendar birth = Calendar.getInstance();
			birth.setTime(birthday);

			
			if(now.get(Calendar.YEAR) > birth.get(Calendar.YEAR)) {
				age = now.get(Calendar.MONTH) + 12 - birth.get(Calendar.MONTH);
				if(age==1) {
					if(birth.get(Calendar.YEAR) % 4 == 0) {
						if(now.get(Calendar.DAY_OF_YEAR) + 366 - birth.get(Calendar.DAY_OF_YEAR) + 1 < 30) {
							age = 0;
						}
					}else {
						if(now.get(Calendar.DAY_OF_YEAR) + 365 - birth.get(Calendar.DAY_OF_YEAR) + 1 < 30) {
							age = 0;
						}
					}
				}
			}else {
				age = now.get(Calendar.MONTH) - birth.get(Calendar.MONTH);
				if(age==1) {
					if(now.get(Calendar.DAY_OF_YEAR) - birth.get(Calendar.DAY_OF_YEAR) + 1 < 30) {
						age = 0;
					}
				}
			}
			
			return age;
		} catch (Exception e) {// 兼容性更强,异常后返回数据
			return 0;
		}
	}
	
	private static int getDayForAgeByBirth(Date birthday) {
		int age = 0;
		try {
			Calendar now = Calendar.getInstance();
			now.setTime(new Date());// 当前时间

			Calendar birth = Calendar.getInstance();
			birth.setTime(birthday);

			
			if(now.get(Calendar.YEAR) > birth.get(Calendar.YEAR)) {
				if(birth.get(Calendar.YEAR) % 4 == 0) {
					age = now.get(Calendar.DAY_OF_YEAR) + 366 - birth.get(Calendar.DAY_OF_YEAR) + 1;
				}else {
					age = now.get(Calendar.DAY_OF_YEAR) + 365 - birth.get(Calendar.DAY_OF_YEAR) + 1;
				}
			}else {
				age = now.get(Calendar.DAY_OF_YEAR) - birth.get(Calendar.DAY_OF_YEAR) + 1;
			}
				
			
			return age;
		} catch (Exception e) {// 兼容性更强,异常后返回数据
			return 0;
		}
	}
	
	public static String getAgeByBirth(Date birthday) {
		int year = getYearForAgeByBirth(birthday);
		if(year==0) {
			int month = getMonthForAgeByBirth(birthday);
			if(month == 0) {
				return getDayForAgeByBirth(birthday) + "天";
			}else {
				return month + "个月";
			}
		}else {
			return year + "岁";
		}
	}
	
	public static Date getRollDay(int day) {
        Date date=new Date();  
        Calendar calendar = Calendar.getInstance();  
        calendar.setTime(date);  
        calendar.add(Calendar.DAY_OF_MONTH, -day);  
        return calendar.getTime();  
		
//		Calendar cl = Calendar.getInstance();
//        cl.roll(Calendar.DATE, -day);
//        return cl.getTime();
        
	}
	
	public static Time getCurrentTime() {
		return new Time(new Date().getTime());
	}
	
	public static Date getInitDate() {
		Date beginDatetime = null;
		try {
			beginDatetime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2000-1-1 00:00:00");
		} catch (Exception e) {
			throw new ServiceException("初始时间获取异常！");
		}
		return beginDatetime;
	}
	
	public static Date creatBeginDate(String dateStr) {
		Date beginDatetime = null;
		try {
			beginDatetime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr.concat(" 00:00:00"));
		} catch (Exception e) {
			throw new ServiceException("时间格式异常！");
		}
		return beginDatetime;
	}
	
	public static Date creatEndDate(String dateStr) {
		Date beginDatetime = null;
		try {
			beginDatetime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(dateStr.concat(" 23:59:59"));
		} catch (Exception e) {
			throw new ServiceException("时间格式异常！");
		}
		return beginDatetime;
	}

	public static Date creatDate(String dateStr) {
		Date beginDatetime = null;
		try {
			beginDatetime =  new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
		} catch (Exception e) {
			throw new ServiceException("时间格式异常！");
		}
		return beginDatetime;
	}

	public static String creatyyyyMMddDate(Date date){
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		return sdf1.format(date);
	}

	public static String getCurrentyyyyMMddDate(){
		Date date = new Date();
		SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
		return sdf1.format(date);
	}
}
